package cn.edu.sgu.www.mhxysy.function.computer.score.impl;

import cn.edu.sgu.www.mhxysy.entity.xingshi.Xingshi;
import cn.edu.sgu.www.mhxysy.entity.xingshi.XingshiFuling;
import cn.edu.sgu.www.mhxysy.entity.xingshi.XingshiXianghun;
import cn.edu.sgu.www.mhxysy.entity.xingshi.XingshiXianghunCategory;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.function.computer.score.ScoreComputer;
import cn.edu.sgu.www.mhxysy.mapper.xingshi.XingshiFulingMapper;
import cn.edu.sgu.www.mhxysy.mapper.xingshi.XingshiMapper;
import cn.edu.sgu.www.mhxysy.mapper.xingshi.XingshiXianghunCategoryMapper;
import cn.edu.sgu.www.mhxysy.mapper.xingshi.XingshiXianghunMapper;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 星石评分计算器
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Component
public class XingshiScoreComputer implements ScoreComputer {

    private final XingshiMapper xingshiMapper;
    private final XingshiFulingMapper xingshiFulingMapper;
    private final XingshiXianghunMapper xingshiXianghunMapper;
    private final XingshiXianghunCategoryMapper xingshiXianghunCategoryMapper;

    @Autowired
    public XingshiScoreComputer(
            XingshiMapper xingshiMapper,
            XingshiFulingMapper xingshiFulingMapper,
            XingshiXianghunMapper xingshiXianghunMapper,
            XingshiXianghunCategoryMapper xingshiXianghunCategoryMapper) {
        this.xingshiMapper = xingshiMapper;
        this.xingshiFulingMapper = xingshiFulingMapper;
        this.xingshiXianghunMapper = xingshiXianghunMapper;
        this.xingshiXianghunCategoryMapper = xingshiXianghunCategoryMapper;
    }

    /**
     * 计算星石评分
     * @param xingshiId 星石ID
     * @return Integer 星石的评分
     */
    @Override
    public Integer apply(String xingshiId) {
        Xingshi xingshi = xingshiMapper.selectById(xingshiId);

        if (xingshi == null) {
            throw new GlobalException(ResponseCode.CONFLICT, "操作失败，星石不存在！");
        }

        /*
         * 星石评分 = 等级评分 + 绿字评分 + 篮字评分（相魂评分）
         * 等级评分 = 星石等级 * 6
         * 绿字评分 = 附灵属性之和 * 2
         */
        AtomicInteger score = new AtomicInteger();

        // 1、等级评分
        // score += 星石等级 * 6
        score.addAndGet(xingshi.getGrade() * 6);

        // 2、绿字评分
        Map<String, Object> columnMap = new HashMap<>(1);

        columnMap.put("xingshi_id", xingshiId);

        List<XingshiFuling> list = xingshiFulingMapper.selectByMap(columnMap);

        int size = list.size();

        if (size > 0) {
            // score += 附灵属性 * 2
            for (XingshiFuling xingshiFuling : list) {
                score.addAndGet(xingshiFuling.getPropertyValue() * 2);
            }

            if (size == 3 && xingshi.getGrade() == 10) {
                // 3、蓝字评分
                QueryWrapper<XingshiXianghun> queryWrapper = new QueryWrapper<>();

                queryWrapper.eq("xingshi_id", xingshiId);

                XingshiXianghun xingshiXianghun = xingshiXianghunMapper.selectOne(queryWrapper);

                if (xingshiXianghun != null) {
                    Integer xianghunId = xingshiXianghun.getXianghunId();
                    XingshiXianghunCategory xingshiXianghunCategory = xingshiXianghunCategoryMapper.selectById(xianghunId);

                    score.addAndGet(xingshiXianghunCategory.getScore());
                }
            }
        }

        return score.get();
    }

}